package com.wakala.employment.fragment.other;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.just.agentweb.core.AgentWeb;
import com.wakala.employment.R;
import com.wakala.employment.core.BaseFragment;
import com.wakala.employment.databinding.FragmentCloseAccountBinding;
import com.wakala.employment.utils.Utils;
import com.wakala.employment.widget.SmoothCheckBox;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.dialog.LoadingDialog;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CloseAccountFragment#} factory method to
 * create an instance of this fragment.
 */
@Page(anim = CoreAnim.none)
public class CloseAccountFragment  extends BaseFragment<FragmentCloseAccountBinding> implements View.OnClickListener {


    private Handler mHandler = new Handler(Looper.getMainLooper());




    @NonNull
    @Override
    protected FragmentCloseAccountBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentCloseAccountBinding.inflate(inflater, container, attachToRoot);
    }

    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("注销账号");
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        binding.btnCloseAccount.setOnClickListener(this);
    }

    @Override
    public void onDestroyView() {
        mHandler.removeCallbacksAndMessages(null);
        super.onDestroyView();
    }

    @Override
    protected void initViews() {
        AgentWeb agentWeb =Utils.createAgentWeb(this, binding.container, "file:///android_asset/closeAccount.html");
        agentWeb.getJsInterfaceHolder().addJavaObject("Android", new AndroidInterface());
        //
        final SmoothCheckBox scb = (SmoothCheckBox) findViewById(R.id.cb_check_box);
        scb.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {
                Log.d("SmoothCheckBox", String.valueOf(isChecked));
            }
        });
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.btn_close_account){
            if(binding.cbCheckBox.isChecked()){
                LoadingDialog mLoadingDialog =  WidgetUtils.getLoadingDialog(getContext());
                mLoadingDialog.show();
                mHandler.postDelayed(() -> {
                    XToastUtils.toast("账号已成功注销");
                    mLoadingDialog.dismiss();
                }, 3000);
            }else {
                XToastUtils.toast("请阅读并同意 河狸帮注销协议");
            }

        }
    }

    public static class AndroidInterface {

        @JavascriptInterface
        public void callAndroid(final String msg) {
            XToastUtils.toast("这是Js调用Android的方法，内容:" + msg);
        }

    }
}